package com.fairytask.ui.item.chat
{
	import com.fairytask.engine.components.text.Label;
	import com.fairytask.managers.ResManager;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	public class ChatListBtnItem extends Sprite
	{
		//容器
		private var _bgBtnSpace		:Sprite;
		private var _iconBtnSpace		:Sprite;
		private var _txtBtnLabel		:Label;
		private var _normalBtnMC		:MovieClip;
		private var _overBtnMC		:MovieClip;
		private var _downBtnMC		:MovieClip;
		private var _hotBtnArea		:Sprite;
		
		//皮肤
		private var _skin			:String	=	"";
		private var _icon			:DisplayObject;
		
		//设定
		private var _btnItemWidth		:Number	=	0;
		private var _btnItemHeight	:Number	=	0;
		private var _currentBtnSkin	:MovieClip;
		private var _currentCheckBoxSkin	:MovieClip;
		
		//数据
		private var _id			:int	=	0;
		private var _label			:String	=	"";
		private var _listData		:Object;
		
		
		public function ChatListBtnItem(btnStr:String,btnW:Number,btnH:Number,id:int,btnItemSkin:String="")
		{
			super();
			_btnItemWidth	=	btnW;
			_btnItemHeight	=	btnH;
			
			setId	=	id;
			
			initGUI(btnStr);
			initEvent();
			skin		=	btnItemSkin;
			buttonMode	=	true;
		}
		
		private function initGUI(str:String):void
		{
			_label				=	str;
			_txtBtnLabel			=	new Label();
			_txtBtnLabel.color		=	"FFFF00";
			_txtBtnLabel.htmlStrByBorder	=	str;
			_bgBtnSpace			=	new Sprite();
			
			_hotBtnArea			=	new Sprite();
			
			_iconBtnSpace			=	new Sprite();
			
			_txtBtnLabel.mouseEnabled	=	_iconBtnSpace.mouseEnabled	=	false;
			
			
			_bgBtnSpace.x=_hotBtnArea.x=_iconBtnSpace.x;
			_bgBtnSpace.y=_hotBtnArea.y=_iconBtnSpace.y;
			addChild(_bgBtnSpace);
			addChild(_txtBtnLabel);
			addChild(_iconBtnSpace);
			
			addChild(_hotBtnArea);
			
			drawBG();
			
			alignTxt();
			
			drawHotArea();
		}
		
		private function initEvent():void
		{
//			if(_id!=2&&_id!=4)
			if(_id!=2)
			{
				addEventListener(MouseEvent.ROLL_OVER,showOver);
				addEventListener(MouseEvent.ROLL_OUT,showOut);
				addEventListener(MouseEvent.MOUSE_DOWN,showDown);
				addEventListener(MouseEvent.MOUSE_UP,showUP);
			}
		}
		
		private function drawBG():void
		{
			_bgBtnSpace.graphics.clear();
			_bgBtnSpace.graphics.beginFill(0xff0000,0);
			if(!_btnItemHeight)
			{
				_btnItemHeight	=	25;
			}
			_bgBtnSpace.graphics.drawRect(0,0,_btnItemWidth,_btnItemHeight);
			_bgBtnSpace.graphics.endFill();
		}
		
		private function drawHotArea():void
		{
			_hotBtnArea.graphics.clear();
			_hotBtnArea.graphics.beginFill(0xff0000,0);
			_hotBtnArea.graphics.drawRect(0,0,_btnItemWidth,_btnItemHeight);
			_hotBtnArea.graphics.endFill();
		}
		
		private function alignTxt():void
		{
			_txtBtnLabel.x	=	(width-_txtBtnLabel.width)/2;
			_txtBtnLabel.y	=	(height-_txtBtnLabel.height)/2 - 1;
		}
		
		private function showOver(e:MouseEvent):void
		{
			if(_currentBtnSkin!=_overBtnMC)
			{
				clearBG();
				_bgBtnSpace.addChild(_overBtnMC);
				_currentBtnSkin	=	_overBtnMC;
			}
		}
		
		private function showOut(e:MouseEvent):void
		{
			if(_currentBtnSkin!=_normalBtnMC)
			{
				clearBG();
				_bgBtnSpace.addChild(_normalBtnMC);
				_currentBtnSkin	=	_normalBtnMC;
			}
		}
		
		private function showDown(e:MouseEvent):void
		{
			if(_currentBtnSkin!=_downBtnMC)
			{
				clearBG();
				_bgBtnSpace.addChild(_downBtnMC);
				_currentBtnSkin	=	_downBtnMC;
			}
		}
		
		private function showUP(e:MouseEvent):void
		{
			if(_currentBtnSkin!=_overBtnMC)
			{
				clearBG();
				_bgBtnSpace.addChild(_overBtnMC);
				_currentBtnSkin	=	_overBtnMC;
			}
		}
		
		private function clearBG():void
		{
			while(_bgBtnSpace.numChildren>0)
			{
				_bgBtnSpace.removeChildAt(0);
			}
		}
		
		override public function set width(val:Number):void
		{
			_btnItemWidth		=	val;
			drawBG();
			resizeSkin();
			alignTxt();
			drawHotArea();
		}
		override public function set height(val:Number):void
		{
			_btnItemHeight		=	val;
			drawBG();
			resizeSkin();
			alignTxt();
			drawHotArea();
		}
		
		private function resizeSkin():void
		{
			if(_skin!="")
			{
				_normalBtnMC.width	=	_overBtnMC.width	=	_downBtnMC.width	=	_btnItemWidth;
				_normalBtnMC.height=	_overBtnMC.height	=	_downBtnMC.height	=	_btnItemHeight;
			}
		}
		
		public function get skin():String
		{
			return _skin;
		}
		
		public function set skin(value:String):void
		{
			clearBG();
			_skin = value;
			if(_skin=="")
			{
				drawBG();
			}else
			{
				var res:MovieClip	=	ResManager.newResClass(_skin) as MovieClip;
				_normalBtnMC			=	res.getChildByName("Button_normal") as MovieClip;
				_overBtnMC			=	res.getChildByName("Button_over") as MovieClip;
				_downBtnMC			=	res.getChildByName("Button_down") as MovieClip;
				resizeSkin();
				_normalBtnMC.x		=	_overBtnMC.x	=	_downBtnMC.x	=	0;
				_normalBtnMC.y		=	_overBtnMC.y	=	_downBtnMC.y	=	0;
				_bgBtnSpace.addChild(_normalBtnMC);
				_currentBtnSkin	=	_normalBtnMC;
			}
		}
		
		public function set icon(value:DisplayObject):void
		{
			while(_iconBtnSpace.numChildren>0)_iconBtnSpace.removeChildAt(0);
			_icon	=	value;
			if(_icon!=null)
			{
				_iconBtnSpace.addChild(_icon);
			}
		}
		
		public function get id():int
		{
			return _id;
		}
		
		private function set setId(value:int):void
		{
			_id = value;
		}
		
		public function get label():String
		{
			return _label;
		}
		
		public function set label(value:String):void
		{
			_label = value;
			_txtBtnLabel.htmlStr	=	_label;
			alignTxt();
		}
		
		public function get listData():Object
		{
			return _listData;
		}
		
		public function set listData(value:Object):void
		{
			_listData = value;
		}
		
		
	}
}